<?php 
global $_GPC, $_W;
$shopid=check_right(2);
if(!$shopid)message("请先选择需要管理的店铺",$this->createWebUrl('shop'),'error');
$shop=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid = '{$_W['uniacid']}' and id='".$shopid."'");
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];

if($operation=="display"){
	$goodpcate = pdo_fetchall("SELECT id,title FROM ".tablename('j_hmoney_goodcategory')." WHERE weid = '{$_W['uniacid']}' and groupid='".$shopid."' order by id desc");
	$goodary = array();
	foreach ($goodpcate as $row) {
		$goodary[$row['id']]=$row['title'];
	}
    $goodary[0]="未分类";
	$condition = "  ";
    $datetype=$_GPC['datetype'] ? $_GPC['datetype'] : 1;
	switch($datetype){
		case "1":
            $starttime=strtotime(date('Y-m-d')." 00:00:00");
            $endtime=strtotime(date('Y-m-d')." 23:59:59");
		break;
		case "2":
            $starttime=strtotime(date('Y-m-d',strtotime("-1 day"))." 00:00:00");
            $endtime=strtotime(date('Y-m-d',strtotime("-1 day"))." 23:59:59");
		break;
		case "3":
            $starttime=strtotime(date('Y-m-d',strtotime("-6 day"))." 00:00:00");
            $endtime=strtotime(date('Y-m-d')." 23:59:59");
		break;
		case "4":
            $starttime=strtotime($_GPC['statrtime']." 00:00:00");
            $endtime=strtotime($_GPC['endtime']." 23:59:59");
		break;
	}
    $condition .= " and a.createtime>='".$starttime."' and a.createtime<='".$endtime."' and a.ispay=1 ";
    $keyword=$_GPC['keyword'];
    $keytype=$_GPC['keytype'];
    if($keytype && $keyword){
        $condition=" and a.orderid in (SELECT id from ".tablename('j_hmoney_order')." where memberno in (SELECT id from ".tablename('j_hmoney_membercard')." where groupid ='".$shopid."' and (cardno like '%".$keyword."%' or wxcardno like '%".$keyword."%' or realname like '%".$keyword."%'))) ";
        $sql="SELECT sum(a.total_fee) as num1,sum(a.num) as num2,b.marketprice,a.goodsn,a.title,b.pcate FROM ".tablename('j_hmoney_ordergoods')." a left join ".tablename('j_hmoney_goods')." b on a.goodid=b.id WHERE a.groupid ='".$shopid."' $condition and a.ispay=1 group by a.goodid order by num1 desc ,num2 desc";
        $goodlist = pdo_fetchall($sql);
        $memberlist=pdo_fetchall("SELECT * from ".tablename('j_hmoney_membercard')." where groupid ='".$shopid."' and (cardno like '%".$keyword."%' or wxcardno like '%".$keyword."%' or realname like '%".$keyword."%')");

    }else{
        if($keyword){
            $condition .=" and ( a.title like '%".$keyword."%' or a.goodsn like '%".$keyword."%' ) ";
        }
        $goodlist = pdo_fetchall("SELECT sum(a.total_fee) as num1,sum(a.num) as num2,b.marketprice,a.goodsn,a.title,b.pcate FROM ".tablename('j_hmoney_ordergoods')." a left join ".tablename('j_hmoney_goods')." b on a.goodid=b.id WHERE a.groupid ='".$shopid."' $condition and a.ispay=1 group by a.goodid order by num1 desc ,num2 desc");
        $condition2=str_replace("a.","",$condition);
        $item = pdo_fetch("SELECT sum(total_fee) as num1,sum(num) as num2 FROM ".tablename('j_hmoney_ordergoods')."  WHERE groupid ='".$shopid."' $condition2 and ispay=1 ");
    }


    $goodary2 = array();
    $goodary3 = array();
    foreach($goodlist as $row){
        if(!isset($goodary2[$row['pcate']])){
            $goodary2[$row['pcate']]=0;
            $goodary3[$row['pcate']]=0;
        }
        $goodary2[$row['pcate']]=$goodary2[$row['pcate']]+$row['num1'];
        $goodary3[$row['pcate']]=intval($goodary3[$row['pcate']])+$row['num2'];
    }


} elseif ($operation == 'outputs') {
    $goodpcate = pdo_fetchall("SELECT id,title FROM ".tablename('j_hmoney_goodcategory')." WHERE weid = '{$_W['uniacid']}' and groupid='".$shopid."' order by id desc");
    $goodary = array();
    foreach ($goodpcate as $row) {
        $goodary[$row['id']]=$row['title'];
    }
    $goodary[0]="未分类";
    $datetype=$_GPC['datetype'] ? $_GPC['datetype'] : 1;
    switch($datetype){
        case "1":
            $starttime=strtotime(date('Y-m-d')." 00:00:00");
            $endtime=strtotime(date('Y-m-d')." 23:59:59");
            break;
        case "2":
            $starttime=strtotime(date('Y-m-d',strtotime("-1 day"))." 00:00:00");
            $endtime=strtotime(date('Y-m-d',strtotime("-1 day"))." 23:59:59");
            break;
        case "3":
            $starttime=strtotime(date('Y-m-d',strtotime("-6 day"))." 00:00:00");
            $endtime=strtotime(date('Y-m-d')." 23:59:59");
            break;
        case "4":
            $starttime=strtotime($_GPC['statrtime']." 00:00:00");
            $endtime=strtotime($_GPC['endtime']." 23:59:59");
            break;
    }
    $condition = " and a.createtime>='".$starttime."' and a.createtime<='".$endtime."' and a.ispay=1 ";

    if($_GPC['keyword']){
        $keyword=$_GPC['keyword'];
        $condition .=" and ( a.title like '%".$keyword."%' or a.goodsn like '%".$keyword."%' ) ";
    }
    $goodlist = pdo_fetchall("SELECT sum(a.total_fee) as num1,sum(a.num) as num2,b.marketprice,a.goodsn,a.title,b.pcate FROM ".tablename('j_hmoney_ordergoods')." a left join ".tablename('j_hmoney_goods')." b on a.goodid=b.id WHERE a.groupid ='".$shopid."' $condition and a.ispay=1 group by a.goodid order by num1 desc ,num2 desc");
    
    require_once "../framework/library/phpexcel/PHPExcel.php";
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getProperties()->setCreator("智慧收银台")
        ->setLastModifiedBy("智慧收银台")
        ->setTitle("数据EXCEL导出")
        ->setSubject("数据EXCEL导出")
        ->setDescription("备份数据")
        ->setKeywords("excel")
        ->setCategory("result file");
    //单号	消费对象	数量	订单金额	茶位	实付金额	优惠	支付方式	收银员	时间	状态	备注
    $excelAry=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","X","T","U","V","W","X","Y","Z");
    $headerAry=array("分类","条码","名称","原单价","销售金额","数量");
    for($i=0;$i<count($headerAry);$i++){
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($excelAry[$i].'1',$headerAry[$i]);
    };
    $num=2;
    foreach($goodlist as $row){
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$num, $goodary[$row['pcate']])
            ->setCellValue('B'.$num, $row['goodsn'])
            ->setCellValue('C'.$num, $row['title'])
            ->setCellValue('D'.$num, $row['marketprice'])
            ->setCellValue('E'.$num, $this->_2money($row['num1']))
            ->setCellValue('F'.$num, $row['num2']);
        $num++;
    }
    $objPHPExcel->getActiveSheet()->setTitle('商品销售记录');
    $objPHPExcel->setActiveSheetIndex(0);
    ob_end_clean();
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition:attachment;filename="salegoods_temp.xls"');
    header('Cache-Control:max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit();
	
}
include $this->template('web/'.$cfg['floder'].'/salegoods');